/* 
 * File:   TestSearchEnginer.cpp
 * Author: vicror
 * 
 * Created on October 20, 2011, 11:27 AM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "TestSearchEnginer.h"
#include <fstream>

CPPUNIT_TEST_SUITE_REGISTRATION(TestSearchEnginerClass);

TestSearchEnginerClass::TestSearchEnginerClass() {
}

TestSearchEnginerClass::~TestSearchEnginerClass() {
}

void TestSearchEnginerClass::setUp() {
    SearchEnginer::SearchEnginer se;
}

void TestSearchEnginerClass::tearDown() {
}

void TestSearchEnginerClass::testGeneral() {
    SearchEnginer::SearchEnginer se;
    SearchEnginer::SearchEnginer sa(se);


    CPPUNIT_ASSERT(se.append("a"));
    CPPUNIT_ASSERT(se.append("iii"));
    CPPUNIT_ASSERT(se.append("ii"));
    CPPUNIT_ASSERT(se.append("toutou"));
    CPPUNIT_ASSERT(se.append("teacher"));
    CPPUNIT_ASSERT(se.append("smaller"));
    CPPUNIT_ASSERT(se.append("nine"));
    CPPUNIT_ASSERT(se.append("to"));
    CPPUNIT_ASSERT(se.append("один"));
    CPPUNIT_ASSERT(se.append("два"));

    CPPUNIT_ASSERT(!sa.append("a"));
    CPPUNIT_ASSERT(!sa.append("iii"));
    CPPUNIT_ASSERT(!sa.append("ii"));
    CPPUNIT_ASSERT(!sa.append("toutou"));
    CPPUNIT_ASSERT(!sa.append("teacher"));
    CPPUNIT_ASSERT(!sa.append("smaller"));
    CPPUNIT_ASSERT(!sa.append("to"));
    CPPUNIT_ASSERT(!sa.append("один"));
    CPPUNIT_ASSERT(!sa.append("два"));


    SearchEnginer::ListString list;
    list = se.hasWord("er");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "teacher");
    CPPUNIT_ASSERT(list[1] == "smaller");
    list = sa.hasWord("to");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "toutou");
    CPPUNIT_ASSERT(list[1] == "to");
    list = sa.hasWord("a");
    CPPUNIT_ASSERT(list.size() == 3);
    CPPUNIT_ASSERT(list[0] == "a");
    CPPUNIT_ASSERT(list[1] == "teacher");
    CPPUNIT_ASSERT(list[2] == "smaller");
    list = sa.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "iii");
    CPPUNIT_ASSERT(list[1] == "ii");
    list = sa.hasWord("ne");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "nine");
    list = sa.hasWord("д");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "один");
    CPPUNIT_ASSERT(list[1] == "два");

    SearchEnginer::SearchEnginer sb;
    SearchEnginer::SearchEnginer sc(sb);


    CPPUNIT_ASSERT(sb.append("lock"));
    CPPUNIT_ASSERT(sb.append("поиск"));
    CPPUNIT_ASSERT(sb.append("замена"));
    CPPUNIT_ASSERT(sb.append("ключ"));
    CPPUNIT_ASSERT(sb.append("вставка"));
    CPPUNIT_ASSERT(sb.append("лямбдалямбда"));
    CPPUNIT_ASSERT(sb.append("лялька"));
    CPPUNIT_ASSERT(sb.append("лола"));
    CPPUNIT_ASSERT(sb.append("имярек"));
    CPPUNIT_ASSERT(sb.append("деньги"));
    list = sc.hasWord("ля");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "лямбдалямбда");
    CPPUNIT_ASSERT(list[1] == "лялька");
    list = sb.hasWord("te");
    CPPUNIT_ASSERT(list.size() == 0);

    SearchEnginer::SearchEnginer sg;
    sg.append(se);
    list = sg.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "iii");
    CPPUNIT_ASSERT(list[1] == "ii");
    list = sg.hasWord("ne");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "nine");
    list = sg.hasWord("д");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "один");
    CPPUNIT_ASSERT(list[1] == "два");
    list = sg.hasWord("ля");
    CPPUNIT_ASSERT(list.size() == 0);
    sg.append(sc);
    list = sg.hasWord("ля");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "лямбдалямбда");
    CPPUNIT_ASSERT(list[1] == "лялька");

    sa.unLink();
    CPPUNIT_ASSERT(sa.append("a"));
    CPPUNIT_ASSERT(sa.append("iii"));
    CPPUNIT_ASSERT(sa.append("ii"));
    CPPUNIT_ASSERT(sa.append("toutou"));
    CPPUNIT_ASSERT(sa.append("teacher"));
    CPPUNIT_ASSERT(sa.append("smaller"));
    CPPUNIT_ASSERT(sa.append("to"));
    CPPUNIT_ASSERT(sa.append("один"));
    CPPUNIT_ASSERT(sa.append("два"));
    sa.unLink();
    sa.link(se);
    CPPUNIT_ASSERT(!sa.append("a"));
    CPPUNIT_ASSERT(!sa.append("iii"));
    CPPUNIT_ASSERT(!sa.append("ii"));
    CPPUNIT_ASSERT(!sa.append("toutou"));
    CPPUNIT_ASSERT(!sa.append("teacher"));
    CPPUNIT_ASSERT(!sa.append("smaller"));
    CPPUNIT_ASSERT(!sa.append("to"));
    CPPUNIT_ASSERT(!sa.append("один"));
    CPPUNIT_ASSERT(!sa.append("два"));

}

void TestSearchEnginerClass::testIO() {
    SearchEnginer::SearchEnginer sg;
    CPPUNIT_ASSERT(sg.append("a"));
    CPPUNIT_ASSERT(sg.append("iii"));
    CPPUNIT_ASSERT(sg.append("ii"));
    CPPUNIT_ASSERT(sg.append("toutou"));
    CPPUNIT_ASSERT(sg.append("teacher"));
    CPPUNIT_ASSERT(sg.append("smaller"));
    CPPUNIT_ASSERT(sg.append("nine"));
    CPPUNIT_ASSERT(sg.append("to"));
    CPPUNIT_ASSERT(sg.append("один"));
    CPPUNIT_ASSERT(sg.append("два"));
    CPPUNIT_ASSERT(sg.append("lock"));
    CPPUNIT_ASSERT(sg.append("поиск"));
    CPPUNIT_ASSERT(sg.append("замена"));
    CPPUNIT_ASSERT(sg.append("ключ"));
    CPPUNIT_ASSERT(sg.append("вставка"));
    CPPUNIT_ASSERT(sg.append("лямбдалямбда"));
    CPPUNIT_ASSERT(sg.append("лялька"));
    CPPUNIT_ASSERT(sg.append("лола"));
    CPPUNIT_ASSERT(sg.append("имярек"));
    CPPUNIT_ASSERT(sg.append("деньги"));


    std::fstream os;
    os.open("basic.dic",  std::fstream::out | std::fstream::trunc );
    os << sg;
    os.close();

    sg.unLink();
    SearchEnginer::SearchEnginer sc(sg);
    
    os.open("basic.dic", std::fstream::in);
    os >> sc;
    os.close();

    SearchEnginer::ListString list;
    list = sg.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "iii");
    CPPUNIT_ASSERT(list[1] == "ii");
    list = sc.hasWord("ne");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "nine");
    list = sg.hasWord("д");
    CPPUNIT_ASSERT(list.size() == 4 );
    CPPUNIT_ASSERT(list[0] == "один");
    CPPUNIT_ASSERT(list[1] == "два");
    CPPUNIT_ASSERT(list[2] == "лямбдалямбда");
    CPPUNIT_ASSERT(list[3] == "деньги");
    list = sc.hasWord("ля");
    CPPUNIT_ASSERT(list.size() == 2);
    CPPUNIT_ASSERT(list[0] == "лямбдалямбда");
    CPPUNIT_ASSERT(list[1] == "лялька");
}
